import { Component, OnInit } from '@angular/core';
import { LoginUser } from '../../../kalend';
import { Router } from '@angular/router';
import { Location } from '@angular/common';
@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.scss']
})
export class MenuComponent implements OnInit {
  menus: Array<any> = [];
  link: any;
  constructor(private router: Router, public Location: Location) { }

  ngOnInit() {
    if (LoginUser.isLogin) {
      this.menus = LoginUser.current.menus.map((item, i) => {
        item.show = false;
        // if (item.name === '用户管理') {
        //   item.show = true;
        // }
        if (i == parseInt(window.localStorage.getItem('index'))) item.show = true;
        return item;
      });
    }
    this.link = this.Location.path().replace('/', '');
  }

  reload(link: any) {
    if (link) {
      this.link = link;
      this.router.navigate(['/'], { skipLocationChange: true })
        .then(() => { this.router.navigate([link]); });
    }
  }

  isActive(url: any) {
    return this.link === url;
  }

  openHandler(i: any): void {
    if (window.localStorage.getItem('index') == i) {
      window.localStorage.removeItem('index');
    } else {
      window.localStorage.setItem('index', i);
    }
    this.menus.forEach((ite, index) => {
      if (index !== i) {
        ite.show = false;
      } else {
        ite.show = true;
      }
    });
  }

}
